//
//  ZLPPicViewController.m
//  TangYi
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 赵连鹏. All rights reserved.
//

#import "ZLPPicViewController.h"
#import <AFNetworking.h>
#import "ZLPTableViewModel.h"
#import "ZLPMyTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "ZLPTopicViewController.h"
#import <MBProgressHUD.h>

@interface ZLPPicViewController ()<UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *topicArr;
@property(nonatomic, retain) MBProgressHUD *hud;

@end

@implementation ZLPPicViewController

- (void)dealloc {
    [_hud release];
    [_tableView release];
    [_topicArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.headTitle;
    
    [self createTableView];
    
    [self isWlan];
    
//    [self createData];
    
}

- (void)isWlan {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == 2 || status == 1) {
            
            [self createData];
            
        } else {
            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
            
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            image.image = [UIImage imageNamed:@"outnetwork.png"];
            [self.view addSubview:image];
            [image release];

        }
        
    }];
}

#pragma mark createTableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 14) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 150;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[ZLPMyTableViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark createData
- (void)createData {
    
    NSLog(@"%@", self.numID);
    if ([self.type isEqualToString:@"webview"]) {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
        [self.view addSubview:webView];
        [webView release];
        
        webView.delegate = self;
        webView.scalesPageToFit = YES;
        
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.labelText = @"loading...";
        
        NSURL *url = [NSURL URLWithString:self.extend];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [webView loadRequest:request];
         
    } else if (self.flag == 0) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:[NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1445349591.654459&app_versions=4.2.2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&ids=%@&oauth_token=8e93baa2127b39cbf64d15bfee8cff56&os_versions=9.0.1&page=0&pagesize=20&screensize=640&track_device_info=iPhone6%%2C2&track_deviceid=A5849E0F-EF33-4B83-80B1-0B846EE0DB3B&track_user_id=1457728&v=7", self.extend] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary *dic = responseObject;
            
            self.topicArr = [NSMutableArray array];
            self.topicArr = [ZLPTableViewModel baseModelByArr:dic[@"data"][@"topic"]];
            
            
            [self.tableView reloadData];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
        }];
        
    } else {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:[NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1445349591.654459&app_versions=4.2.2&category=%@&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&oauth_token=8e93baa2127b39cbf64d15bfee8cff56&os_versions=9.0.2&page=0&pagesize=20&screensize=640&track_device_info=iPhone6%%2C2&track_deviceid=A5849E0F-EF33-4B83-80B1-0B846EE0DB3B&track_user_id=1457728&v=7", self.numID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary *dic = responseObject;
            
            self.topicArr = [NSMutableArray array];
            self.topicArr = [ZLPTableViewModel baseModelByArr:dic[@"data"][@"topic"]];
            
            
            [self.tableView reloadData];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
        }];
    }
    
    
}




#pragma mark tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.topicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLPMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    ZLPTableViewModel *model = [[ZLPTableViewModel alloc] init];
    model = self.topicArr[indexPath.row];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    cell.titleLable.text = model.title;
    cell.likeView.image = [UIImage imageNamed:@"like.png"];
    cell.countLabel.text = model.likes;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLPTopicViewController *topicVC = [[ZLPTopicViewController alloc] init];
    [self.navigationController pushViewController:topicVC animated:YES];
    topicVC.numID = [self.topicArr[indexPath.row] numID];
    topicVC.topic = self.topicArr[indexPath.row];
}

#pragma mark webView的协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.hud removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
